function chooseSide() {
	var that;
	this.getParams = new getParams();
}

chooseSide.prototype = {
	team_id:'',  // 1: 红队 2: 蓝队
	params:'',
	init_url:"../mobile/choosePage",
	join_url:"../mobile/join",
	open_id:"",
	meet_id:"",
	sequence:"",
	team:["red","blue"],
	round:'',
	init:function() {
		that = this;
		that.params = window.location.search;
		that.meet_id = that.getParams.getQueryString("meet_id");
		that.open_id = that.getParams.getQueryString("openid");
		that.sequence = that.getParams.getQueryString("sequence");
		that._gameInit();
		that.bindEvent();
	},
	bindEvent:function() {
		$("#join-blue,#join-red").on("click",that._chooseTeam);
	},
	// 游戏初始化
	_gameInit:function() {
		var data = {
			meet_id:that.meet_id,
			sequence:that.sequence,
			openid :that.open_id
		};
		that._request_data(that.init_url,data,that._gameInitCallback,that._gameInitErrorCallback);
	},
	_gameInitCallback:function(backdata) {
		that.getParams.hideLoadMask();
		var redInfo = backdata.redInfo;
		var blueInfo = backdata.blueInfo;
		var gameInfo = backdata.gameInfo;
		var res = parseInt(backdata.reCode);
		if(res == 1) {
			that.round = gameInfo.round;
			$("#team-left").find(".team-img").attr("src",redInfo.team_image);
			$("#team-left").find(".team-name").html(redInfo.team_name);
			$("#team-right").find(".team-img").attr("src",blueInfo.team_image);
			$("#team-right").find(".team-name").html(blueInfo.team_name);
			//轮数的显示
			if(that.round<=9) {
				$("#round").addClass("number"+that.round);
			} else {
				var first = parseInt(that.round/10);
				var second = that.round % 10;
				$("#round").addClass("number"+first);
				$("#round").after('<span class="round-num bgimg number'+second+'"></span>');
			}

		} else if(res <0)
		{
			alert(backdata.msg);
		}
	},
	_gameInitErrorCallback:function(ret) {

	},

	//选择队伍
	_chooseTeam:function() {
		that.team_id = $(this).attr("data-value");
		var data = {
				meet_id:that.meet_id,
				open_id:that.open_id,
				sequence:that.sequence,
				team:that.team[that.team_id],
			    round:that.round,
			};
		that._request_data(that.join_url,data,that._chooseTeamCallback,that._errorChooseTeamCallback);
	},
	_chooseTeamCallback:function(ret) {
		var teamInfo = ret.teamInfo;
		var flag = parseInt(ret.res);
		// success
		if(flag == 0) {
			var txt = "队伍已满员,无法进入";
			that.getParams.showTips(txt);
		}
		// join success
		if(flag == 1) { //成功加入队伍
			window.location.href = "waitPage" + that.params + "&team=" + that.team[that.team_id]+"&round="+ that.round;
		}
		//已选择队伍，中途退出
		if(flag == 2) {
			if (that.team[that.team_id] == ret.team) {
				window.location.href = "waitPage" + that.params + "&team=" + that.team[that.team_id] + "&round=" + that.round;
			} else {
				var txt = "请选择另一队";
				that.getParams.showTips(txt);
			}
		}
		//游戏正在进行中
		if(flag == 3) {
			if (that.team[that.team_id] == ret.team) {
				window.location.href = "shake" + that.params + "&team=" + that.team[that.team_id] + "&team_img=" + teamInfo.team_image + "&team_name=" + teamInfo.team_name + "&round=" + that.round;
			} else {
				var txt = "游戏已开始！请选择另一队";
				that.getParams.showTips(txt);
			}
		}
		// already started
		if(flag == 4) {
			var txt = "游戏未开始，请注意大屏提示";
			that.getParams.showTips(txt);
		}
		if(flag < 0) {
			var txt = ret.msg;
			that.getParams.showTips(txt);
		}
		return;
	},
	_errorChooseTeamCallback:function(ret) {
	},
	_request_data:function(url,data,callback,errorCallback) {
		$.ajax({
			type:"post",
			url:url,
			dataType:"json",
			data:data,
			success:function(ret) {
				callback && callback(ret);
			},
			error:function() {
				errorCallback && errorCallback();
			}
		})
	}
}

$(function() {
	var obj = new chooseSide();
		obj.init();
})